package com.aaron.gesturehelper;

import android.content.Context;
import android.content.Intent;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.AttributeSet;

public class ColorPickerPreference extends Preference implements ColorPickerDialog.OnColorChangedListener,OnPreferenceClickListener {

	public ColorPickerPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnPreferenceClickListener(this);
	}

	public ColorPickerPreference(Context context) {
		super(context);
		setOnPreferenceClickListener(this);
	}

	@Override
	public void colorChanged(int color) {
		int rgb = color&0xFFFFFF;
		MyConfiguration.getInstance(getContext()).setHexInt(getKey(), Integer.toHexString(rgb));
		Intent intent = new Intent(Contants.INTENT_CONFIGURATION_CHANGED);
		intent.putExtra(getKey(), true);
		getContext().sendBroadcast(intent);
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		String defValue;
		if(getKey().equals(MyConfiguration.GESTURE_COLOR)) {
			defValue = "FFFF00";
		} else {
			defValue = "0000FF";
		}
		int initcolor = MyConfiguration.getInstance(getContext()).getHexInt(getKey(), defValue);
		new ColorPickerDialog(getContext(), this, initcolor|0xFF000000).show();
		return true;
	}

}
